{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 查看版本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前的时间: 2021-11-27 21:39:54\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "print(\"当前的时间:\",time.strftime(\"%Y-%m-%d %H:%M:%S\", time.localtime()) )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch的版本:\t 1.7.1\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "print(\"torch的版本:\\t\",torch.__version__)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 创建张量"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### pytorch一般到4阶张量 5阶都很几乎用不到"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1, 2, 3])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#从列表中创建\n",
    "torch.tensor([1,2,3])#一阶张量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-1,  1],\n",
       "        [ 1, -1]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.tensor([[-1,1],[1,-1]])#二阶张量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1.]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#创建单位张量\n",
    "torch.ones([3,4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0., 0., 0., 0.],\n",
       "        [0., 0., 0., 0.],\n",
       "        [0., 0., 0., 0.]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#0张量\n",
    "torch.zeros([3,4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[9.5461e-01, 4.4377e+27, 1.7975e+19, 4.6894e+27],\n",
       "        [7.9463e+08, 3.2604e-12, 2.6209e+20, 4.1641e+12],\n",
       "        [1.9434e-19, 3.0881e+29, 6.3828e+28, 1.4603e-19]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.empty(3,4)#这个时候用的是随机的数字填充的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1, 3, 5, 7, 9])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.arange(1,10,2)#start end step"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 1.0000, 13.2500, 25.5000, 37.7500, 50.0000])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.linspace(1,50,5)#start end 数字数量   等差数列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 1.0000,  6.4444, 11.8889, 17.3333, 22.7778, 28.2222, 33.6667, 39.1111,\n",
       "        44.5556, 50.0000])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.linspace(1,50,10)#start end 数字数量   等差数列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.0152, 0.7842, 0.1790],\n",
       "        [0.9146, 0.3101, 0.1540]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#随机张量  这个用的最多\n",
    "torch.rand(2,3)#这里是2行3列 并不是范围在2-3  并且数字是0-1范围内"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.2887, 0.3392, 0.2049],\n",
       "        [0.2422, 0.2483, 0.3940]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.rand([2,3])#这种用中括号合起来的方式也是可以的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.5248, 0.2554, 0.5206],\n",
       "        [0.3804, 0.3474, 0.6020]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.rand((2,3))#这种用括号合起来的方式也是可以的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[4, 7, 2, 3],\n",
       "        [4, 2, 6, 7],\n",
       "        [5, 3, 5, 4]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.randint(2,8,[3,4])#low high shape  左闭右开区间[low,high)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.1725, -1.7573,  0.2997,  1.3757],\n",
       "        [-0.1377, -0.2441, -0.5707,  0.2746],\n",
       "        [ 2.2491,  1.1024, -0.2185,  1.4670]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.randn([3,4])#均值为0方差为1的数据"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Pytorch中的tensor属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'torch.Tensor'>\n",
      "0\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "tensor([0])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#1.获取tensor中的数据\n",
    "#如果tensor.item()中只有一个数据时，可以直接拿到数据\n",
    "import numpy as np\n",
    "a=torch.tensor(np.arange(1))\n",
    "print(type(a))\n",
    "print(a.item())#这样可以直接拿到里面的元素\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-2.2000],\n",
       "        [ 7.3000],\n",
       "        [-1.5000]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#tensor张量转化为numpy数组\n",
    "a=torch.tensor([[-2.2],[7.3],[-1.5]])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-2.2]\n",
      " [ 7.3]\n",
      " [-1.5]] <class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "b=a.numpy()\n",
    "print(b,type(b))#这样就完成了tensor和numpy的转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([3, 1])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#获取形状\n",
    "a.size()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.float32"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#获取类型\n",
    "a.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#获取阶数\n",
    "a.dim()#2阶  3行1列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[1, 2],\n",
      "        [8, 4],\n",
      "        [7, 6]]) <class 'torch.Tensor'> \n",
      "x.dim:\t <built-in method dim of Tensor object at 0x7fa2b1d539d8> \n",
      "x.size():\t torch.Size([3, 2]) \n",
      "x.dtype(): torch.int64\n"
     ]
    }
   ],
   "source": [
    "#形状的修改\n",
    "x=torch.tensor([[1,2],[8,4],[7,6]])\n",
    "print(x,type(x),'\\nx.dim:\\t',x.dim,'\\nx.size():\\t',x.size(),'\\nx.dtype():',x.dtype)#3行2列 ，6阶"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1, 2, 8],\n",
       "        [4, 7, 6]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#3行2列  可以用2行3列来展示\n",
    "x.view(2,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([2, 3])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.view(2,3).size()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x: tensor([[1, 2],\n",
      "        [8, 4],\n",
      "        [7, 6]]) \n",
      "x的类型: <class 'torch.Tensor'> \n",
      "x.dim:\t <built-in method dim of Tensor object at 0x7fa2b1d539d8> \n",
      "x.size():\t torch.Size([3, 2]) \n",
      "x.dtype(): torch.int64\n"
     ]
    }
   ],
   "source": [
    "print(\"x:\",x,'\\nx的类型:',type(x),'\\nx.dim:\\t',x.dim,'\\nx.size():\\t',x.size(),'\\nx.dtype():',x.dtype)#此时x还是没有改变的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x.t(): tensor([[1, 8, 7],\n",
      "        [2, 4, 6]]) \n",
      "x.t()的类型: <class 'torch.Tensor'> \n",
      "x.dim:\t <built-in method dim of Tensor object at 0x7fa3c009d948> \n",
      "x.t().size():\t torch.Size([2, 3]) \n",
      "x.t().dtype(): torch.int64\n"
     ]
    }
   ],
   "source": [
    "#转置\n",
    "print(\"x.t():\",x.t(),'\\nx.t()的类型:',type(x.t()),'\\nx.dim:\\t',x.t().dim,'\\nx.t().size():\\t',x.t().size(),'\\nx.t().dtype():',x.t().dtype)#此时x还是没有改变的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1, 8, 7],\n",
       "        [2, 4, 6]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.transpose(0,1)#这个也是转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x:\t tensor([[[1, 2, 3],\n",
      "         [4, 5, 6]],\n",
      "\n",
      "        [[2, 2, 3],\n",
      "         [6, 5, 4]],\n",
      "\n",
      "        [[1, 5, 4],\n",
      "         [9, 3, 4]],\n",
      "\n",
      "        [[5, 6, 3],\n",
      "         [1, 4, 2]]]) <class 'torch.Tensor'> torch.Size([4, 2, 3])\n",
      "y:\t tensor([[[1, 4],\n",
      "         [2, 6],\n",
      "         [1, 9],\n",
      "         [5, 1]],\n",
      "\n",
      "        [[2, 5],\n",
      "         [2, 5],\n",
      "         [5, 3],\n",
      "         [6, 4]],\n",
      "\n",
      "        [[3, 6],\n",
      "         [3, 4],\n",
      "         [4, 4],\n",
      "         [3, 2]]]) <class 'torch.Tensor'> torch.Size([3, 4, 2])\n"
     ]
    }
   ],
   "source": [
    "#使用permute可以转更多的轴，使用transpose需要好几道程序\n",
    "x=torch.tensor([\n",
    "    [[1,2,3],[4,5,6]],\n",
    "    [[2,2,3],[6,5,4]],\n",
    "    [[1,5,4],[9,3,4]],\n",
    "    [[5,6,3],[1,4,2]]\n",
    "])\n",
    "print(\"x:\\t\",x,type(x),x.size())  \n",
    "y=x.transpose(1,2).transpose(1,0)#[4,2,3]->[4,3,2]->[3,4,2]\n",
    "print(\"y:\\t\",y,type(y),y.size())  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[[1, 4],\n",
      "         [2, 6],\n",
      "         [1, 9],\n",
      "         [5, 1]],\n",
      "\n",
      "        [[2, 5],\n",
      "         [2, 5],\n",
      "         [5, 3],\n",
      "         [6, 4]],\n",
      "\n",
      "        [[3, 6],\n",
      "         [3, 4],\n",
      "         [4, 4],\n",
      "         [3, 2]]]) <class 'torch.Tensor'> torch.Size([3, 4, 2])\n"
     ]
    }
   ],
   "source": [
    "z=x.permute(2,0,1)##[4,2,3]->[3,4,2]  一步到位\n",
    "print(z,type(z),z.size()) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[1],\n",
       "         [2],\n",
       "         [3]]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=torch.tensor([[[1],[2],[3]]])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([1, 3, 1])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.size()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1, 2, 3])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b=a.squeeze()#squeeze()压缩维度\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([3])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.size()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1, 2, 3]])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c=b.unsqueeze(0)#增加维度\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([1, 3])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c.size()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1],\n",
       "        [2],\n",
       "        [3]])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d=b.unsqueeze(1)#增加维度,这个是在1的维度上增加\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1., 1., 1.],\n",
       "        [1., 1., 1.]])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=torch.ones([2,3])#生成指定类型的矩阵\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.float32"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1, 1, 1],\n",
       "        [1, 1, 1]], dtype=torch.int32)"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=torch.ones([2,3],dtype=torch.int32)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.int32"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1, 2, 5],\n",
       "        [7, 5, 3],\n",
       "        [8, 6, 3],\n",
       "        [9, 5, 3]])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#切片操作\n",
    "x=torch.tensor([[1,2,5],[7,5,3],[8,6,3],[9,5,3]])\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([4, 3]) torch.int64\n"
     ]
    }
   ],
   "source": [
    "print(x.size(),x.dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([2, 5, 6, 5])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[:,1]#切片操作跟numpy里面的一模一样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[-1.3969,  0.4999,  1.5894,  1.8638],\n",
       "         [-0.6353,  0.3220,  0.9471, -0.0023],\n",
       "         [-0.5656,  0.1495,  0.1781, -0.5484]],\n",
       "\n",
       "        [[-0.8573, -0.0102,  0.9279, -0.5100],\n",
       "         [-0.8563,  0.2930,  0.6725, -1.4805],\n",
       "         [-0.0440, -0.1532, -0.3623,  0.2215]]])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=torch.randn(2,3,4)\n",
    "a#均值为0方差为1的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([2, 3, 4]) torch.float32\n"
     ]
    }
   ],
   "source": [
    "print(a.size(),a.dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[-1.3969,  0.4999]],\n",
       "\n",
       "        [[-0.8573, -0.0102]]])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[:,:1,:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([2, 1, 2])"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[:,:1,:2].size()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-1.3969,  0.4999, -0.8573, -0.0102]])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[:,:1,:2].reshape(1,4)#可以通过reshape去修改维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
